<?php 
class OrederController extends CommonController{
		private $data;


		public function __construct(){
			parent::__construct();
			if(!isset($_SESSION['uid']) || !isset($_SESSION['username'])){
			//跳转
			$this->error("请先登入","Storelist/index");
			}

			$this->data = K('Oreder');

		}



		public function index(){

			$uid = Q('session.uid');
			//获得商铺id
			$stid = Q('get.stid',0,"intval");

			//获得商铺的信息
			$storeInfo = K('Store')->get_one_data(array('stid'=>$stid));

			$this->assign('storeInfo',$storeInfo);
			//获得商品信息
			
			$defaultGetall = $_SESSION['shop'][$stid];

			$this->assign('defaultGetall',$defaultGetall);

			//用户的收货地址
			$defaultUser = K('Address')->get_data(array('uid'=>$uid));
			$this->assign('defaultUser',$defaultUser);

			$attr = explode(',',$storeInfo['favorable']);
			
			$this->assign('attr',$attr);
			





			$this->display();
		}

		/**
		 * [ajax_oreder 加]
		 * @return [type] [description]
		 */
		public function ajax_oreder(){
			if(IS_AJAX){
				// 获得商品id
				$stid = Q('get.stid',0,"intval");
				$gsid = Q('post.gsid');
				$sid = Q('post.sid');
				$nub = Q('post.nub');
				// 查找所对应的商品
				$result = K('Shop')->get_one_data(array('sid'=>$sid));

				$data = array( 
					'id' => $sid,
				 	'name'=>$result['shopname'],// 商品名称
					'num' => 1, // 商品数量
					'price' => $result['price'], // 商品价格
		 			);
				 Cart::add($data); // 添加到购物车
				$_SESSION['shop'][$stid] = Cart::getAllData();

				$defaultGetall = $_SESSION['shop'][$stid];
				

				if(!empty($defaultGetall)){
					$this->ajax($defaultGetall);
				}
			}
		}
		/**
		 * [ajax_oreders 减]
		 * @return [type] [description]
		 */
		public function ajax_oreders(){
			if(IS_AJAX){
				// 获得商铺id
				$stid = Q('get.stid',0,"intval");
				//自动生成的sid
				$gsid = Q('post.gsid');
				// 商品的ID
				$sid = Q('post.sid');
				//数量
				$nub = Q('post.nub');
				
				

				
				// 查找所对应的商品
				$result = K('Shop')->get_one_data(array('sid'=>$sid));

				$data = array( 
					'id' => $sid,
				 	'name'=>$result['shopname'],// 商品名称
					'num' => -1, // 商品数量
					'price' => $result['price'], // 商品价格
		 			);
				 Cart::add($data); // 添加到购物车
				$_SESSION['shop'][$stid] = Cart::getAllData();

				$defaultGetall = $_SESSION['shop'][$stid];
				if($nub<=0){
					unset($_SESSION['shop'][$stid]['goods'][$gsid]);
					Cart::del($gsid);
					
				}


				if(!empty($defaultGetall)){
					$this->ajax($defaultGetall);
				}
			}
		}

		public function submit_oreder(){
			if(IS_AJAX){
				//用户的UID
				$uid = Q('session.uid');
				// 用户的的收货地址
				$address = Q('post.userInfo');
				//发票信息信息
				$invoice = Q('post.invoice');
				//备注信息
				$remark = Q('post.remark');
				//购物车的信息
				$stid = Q('get.stid',0,'intval');
				$defaultGetall = $_SESSION['shop'][$stid];
				
				$data = array(
				'Clearing'=>$defaultGetall['total'],
				'shopnub' => $defaultGetall['total_rows'],

				'stid'=>$stid,

				'uid'=>$uid,

				'invoice'=>$invoice,

				'remark'=>$remark,

				'address'=>$address,

				);

			if($this->data->add_data($data)){
				unset($_SESSION['shop']);
				$this->ajax(1);
				Cart::del($gsid);

				}

			}

		}
}




 ?>